package com.example.administrator.foodrowfresh.jiao.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.foodrowfresh.R;
import com.example.administrator.foodrowfresh.jiao.PromotionContentActivity;
import com.example.administrator.foodrowfresh.jiao.modle.PromotionItemModel;
import com.example.administrator.foodrowfresh.superBase.MyBaseAdapter;

import org.xutils.x;

import java.util.List;
import java.util.Map;

/**
 * Created by Bob on 2016/4/20.
 */
public class PromotionItemAdapter extends MyBaseAdapter<PromotionItemModel.DataBean> {
    private Context mContext;
    private List<PromotionItemModel.DataBean> mDatas;

    /////
    private List<Map<String, String>> list1;
    private LayoutInflater inflater;
    private PromotionItemGVAdapter promotionItemGVAdapter;

    public PromotionItemAdapter(List<PromotionItemModel.DataBean> datas, List<Map<String, String>> list1, Context context) {
        super(datas, context);
        mContext = context;
        mDatas = datas;
        inflater = LayoutInflater.from(context);
        this.list1 = list1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHodler hodler;
        if (convertView == null) {
            hodler = new ViewHodler();
            convertView = inflater.from(mContext).inflate(R.layout.promotion_activity_list_item, parent, false);
            hodler.mGridView = (GridView) convertView.findViewById(R.id.promotion_activity_gv);

            //子线程下载json
            hodler.image = (ImageView) convertView.findViewById(R.id.promotion_activity_image);
            hodler.title = (TextView) convertView.findViewById(R.id.promotion_activity_tv_title);
            hodler.begin_time = (TextView) convertView.findViewById(R.id.promotion_activity_time);
            hodler.smalltitle = (TextView) convertView.findViewById(R.id.promotion_activity_tv_smalltitle);
            convertView.setTag(hodler);
            hodler.mGridView.setTag(position);
        } else {
            hodler = (ViewHodler) convertView.getTag();
        }
        hodler.title.setText(mDatas.get(position).getTitle());
        hodler.begin_time.setText(mDatas.get(position).getBegin_time());
        hodler.smalltitle.setText(mDatas.get(position).getTitle());
        String image = mDatas.get(position).getImage();
        x.image().bind(hodler.image, image);

        promotionItemGVAdapter = new PromotionItemGVAdapter(list1, mContext);

        hodler.mGridView.setAdapter(promotionItemGVAdapter);
        PromotionAdapterGridView.setGridViewHeightBasedOnChildren(hodler.mGridView);

        //网格监听点击
        hodler.mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int tagPosition = (int) hodler.mGridView.getTag();

                Intent intent = new Intent(mContext, PromotionContentActivity.class);
                Toast.makeText(mContext, " -------------" + intent, Toast.LENGTH_SHORT).show();
                Bundle bundle = new Bundle();
                bundle.putString("id",list1.get(position).get("id"));
                intent.putExtra("bundle", bundle);
                mContext.startActivity(intent);

                promotionItemGVAdapter.notifyDataSetChanged();

            }
        });
        return convertView;
    }


    public class ViewHodler {
        GridView mGridView;
        TextView title, begin_time, smalltitle;
        ImageView image;
    }
}
